package com.yonyou.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Component;

import com.yonyou.domain.Prize;
import com.yonyou.status.CommnonStatus;

@Component
public class PrizeDAO {
	@Autowired
	public MongoTemplate mongoTemplate;

	@Autowired
	public GridFsTemplate gridFsTemplate;

	/**
	 * 查询奖项列表
	 * 
	 * @return
	 */
	public List<Prize> list(String meeting_id) {
		Criteria criteria = Criteria.where("meeting_id").is(meeting_id);
		criteria.andOperator(Criteria.where("dr").is(CommnonStatus.SAVSTATUS));
		Query query = Query.query(criteria);
		return mongoTemplate.find(query, Prize.class);
	}

	/**
	 * 新增奖项
	 * 
	 * @param prize
	 */
	public void save(Prize prize) {
		mongoTemplate.insert(prize);
	}

	/**
	 * 修改奖项
	 * 
	 * @param prize
	 */
	public void update(Prize prize) {
		Query query = Query.query(Criteria.where("id").is(prize.getId()));
		Update update = Update.update("prize_name", prize.getPrize_name());
		update.set("prizes", prize.getPrizes());
		update.set("is_allowed_duplicate", prize.getIs_allowed_duplicate());
		update.set("draw_mode", prize.getDraw_mode());
		update.set("list_ofWinners", prize.getList_ofWinners());
		update.set("shake_style", prize.getShake_style());
		update.set("draw_user_source", prize.getDraw_user_source());
		update.set("user_screen", prize.getUser_screen());
		update.set("filename", prize.getFilename());
		update.set("dr", prize.getDr());
		update.set("ts", prize.getTs());
		update.set("list", prize.getList());
		mongoTemplate.updateFirst(query, update, Prize.class);
	}

	/**
	 * 删除奖项信息
	 * 
	 * @param prize
	 */
	public void delete(Prize prize) {
		Query query = Query.query(Criteria.where("id").is(prize.getId()));
		Update update = Update.update("dr", CommnonStatus.DELSTATUS);
		mongoTemplate.updateFirst(query, update, Prize.class);
	}

}
